define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'bill/bill/index' + location.search,
                    add_url: 'bill/bill/add',
                    multi_url: 'bill/bill/multi',
                    import_url: 'bill/bill/import',
                    table: 'shopro_order_bill',
                }
            });
            var table = $("#table");
            //分账总金额
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log(":data");
                console.log(data);
                //这里我们手动设置底部的值
                $("#sub_total_money").text(data.sub_total_money);
                $("#ther_money").text(data.ther_money);
                $("#no_money").text(data.no_money);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'user.nickname', title: __('User.nickname')},
                        {field: 'order_sn', title: __('Order_sn')},
                        {field: 'total_amount', title: __('Total_amount'), operate:false},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'goods_type', title: __('Goods_type'), searchList: {"5":__('Goods_type 5'),"2":__('Goods_type 2'),"1":__('Goods_type 1'),"6":__('Goods_type 6'),"7":__('Goods_type 7')}, formatter: Table.api.formatter.normal},
                        // {field: 'all_account', title: __('All_account'),operate:false},
                        {field: 'fz_title', title: __('分账账号信息'),operate:false},
                        {field: 'percent', title: __('分账比例'),operate:false},
                        {field: 'fz_money', title: __('分账金额'),operate:false},
                        // {field: 'today_sub_account', title: __('Today_sub_account'), operate:false},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'coupon_status', title: __('Coupon_status'), searchList: {"0":__('Coupon_status 0'),"1":__('Coupon_status 1')}, formatter: Table.api.formatter.status},

                        {field: 'checktime', title: __('Checktime'), operate:false, addclass:'datetimerange', autocomplete:false},
                        {field: 'fz_time', title: __('分账时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                // {
                                //     name: 'detail',
                                //     text: __('Detail'),
                                //     icon: 'fa fa-list',
                                //     classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                //     url: 'bill/bill/detail'
                                // },
                                // {
                                //     name: 'check',
                                //     text: __('审核'),
                                //     visible: function (row) {
                                //         if (row.status == 0) {
                                //             // return true 代表显示
                                //             return true;
                                //         }
                                //     },
                                //     confirm: '确定审核吗？',
                                //     classname: 'btn btn-warning btn-xs  btn-ajax',
                                //     url: 'bill/bill/check',
                                //     success:function(){
                                //         table.bootstrapTable('refresh',{silent: true });
                                //     }
                                // },
                            ],

                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //批量分账
            $(".btn-sub_account").click(function(){
                var ids = Table.api.selectedids(table);
                layer.confirm('确定要批量分账选中项?', {btn: ['是','否'] },
                    function(index){
                        layer.close(index);
                        $.post("/qKbDvjJcSX.php/bill/bill/multi_status", {ids:ids ,field:"status", action:'success', reply:''},function(response){
                            if(response.code == 1){
                                Toastr.success(response.msg)
                                $(".btn-refresh").trigger('click');
                            }else{
                                Toastr.error(response.msg)
                            }
                        }, 'json')
                    },
                    function(index){
                        layer.close(index);
                    }
                );
            });
            //批量入账
            $(".btn-enter_account").click(function(){
                var ids = Table.api.selectedids(table);
                layer.confirm('确定要批量入账选中项?', {btn: ['是','否'] },
                    function(index){
                        layer.close(index);
                        $.post("/qKbDvjJcSX.php/bill/bill/multi_status", {ids:ids ,field:"coupon_status", action:'success', reply:''},function(response){
                            if(response.code == 1){
                                Toastr.success(response.msg)
                                $(".btn-refresh").trigger('click');
                            }else{
                                Toastr.error(response.msg)
                            }
                        }, 'json')
                    },
                    function(index){
                        layer.close(index);
                    }
                );
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        subaccount_url: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
